// pages/goodsDetail/goodsDetail.js
const { getGoodsDetail } = require('../../api/index');
Page({

	showSpecPopup(e) {
		const type = e.currentTarget.dataset.type;

		const selectedGood = this.data.goodsDetail;
		const initialSpecs = selectedGood.specification;

		const initialPrice = initialSpecs.reduce((sum, spec) => {
			return sum + Number(spec.options[0].additional_price);
		}, Number(selectedGood.price));
		const newGood = {
			...selectedGood,
			selectedNum: 1,
			selectedPrice: initialPrice,
			totalPrice: initialPrice,
			selectedOptions: initialSpecs.map(s => s.options[0].title).join('、 ')
		};
		this.setData({
			selectedGoods: newGood,
			goodVisible: true,
			actionType: type,
		});
	},
	onGoodVisibleChange(e) {
		this.setData({
			goodVisible: e.detail.visible,
		});
	},
	onSelectedChange(e) {
		const specIndex = e.currentTarget.dataset.specIndex; // 获取规格索引
		const selectedIndex = e.detail.value; // 获取选中的选项索引
		// 使用setData更新对应规格的selectedIndex
		this.setData({
			[`goodsDetail.specification[${specIndex}].selectedIndex`]: selectedIndex
		});
		const specifications = this.data.goodsDetail.specification;
		const total = specifications.reduce((sum, spec) => {
			const selectedOption = spec.options[spec.selectedIndex];
			return sum + Number(selectedOption.additional_price);
		}, Number(this.data.goodsDetail.price));

		// 生成选项描述
		const optionsDesc = specifications.map(spec => {
			return spec.options[spec.selectedIndex].title;
		}).join('、 ');

		this.setData({
			[`selectedGoods.selectedPrice`]: total,
			[`selectedGoods.totalPrice`]: total*this.data.selectedGoods.selectedNum,
			[`selectedGoods.selectedOptions`]: optionsDesc
		});
	},
	onGoodStepperChange(e) {
		const { value } = e.detail
		this.setData({
			[`selectedGoods.selectedNum`]: value,
			[`selectedGoods.totalPrice`]:  value * this.data.selectedGoods.selectedPrice
		});
	},
	onSureTap(e) {
		const app = getApp()
		const newItem = {
			...this.data.selectedGoods,
		};
		if (this.data.actionType === 'add'){
			app.globalData.goodsCart = app.globalData.goodsCart || {totalNum: 0,totalPrice: 0,goods: []};
			app.globalData.goodsCart =
				{
					totalNum: app.globalData.goodsCart.totalNum + newItem.selectedNum,
					totalPrice: (Number(app.globalData.goodsCart.totalPrice) +
						Number(newItem.selectedPrice) * newItem.selectedNum),
					// 添加新商品到数组
					goods: [...app.globalData.goodsCart.goods, newItem]
				}
			console.log(app.globalData.goodsCart);
			wx.showToast({
				title: '添加成功',
				icon: 'success',
				duration: 2000
			});
		}else {
			app.globalData.goodsOrder = {
				totalNum: newItem.selectedNum,
				totalPrice: Number(newItem.selectedPrice) * newItem.selectedNum,
				goods: [newItem]
			};
			wx.navigateTo({
				url: '/pages/goodsOrderSettlement/goodsOrderSettlement',
			});
		}
	},
	/**
	 * 页面的初始数据
	 */
	data: {


		actionType: 'add',
		goodVisible: false,

		selectedGoods:null,
		goodsDetail: null,
		banners: [
			{
				"id": "1",
				"value": "https://tdesign.gtimg.com/mobile/demos/swiper1.png"
			},
			{
				"id": "2",
				"value": "https://tdesign.gtimg.com/mobile/demos/swiper1.png"
			}
		],
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		if (options.id) {
			const id = options.id
			getGoodsDetail(id).then(res => {
				this.setData({
					goodsDetail: res.goodsDetail
				})
			})
		} else {
			const app = getApp()
			this.setData({
				goodsDetail: app.globalData.selectedGoods
			})
		}
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})